package com.ruoyi.yipaiju.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

@Component
public class FontTestRunner implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(FontTestRunner.class);
    private final ResourceLoader resourceLoader;

    @Autowired
    public FontTestRunner( ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    public Resource getFontResource() {
        // 使用ResourceLoader获取类路径下的资源，classpath:表示从类路径加载
        return resourceLoader.getResource("classpath:fonts/SimHei.ttf");
    }
    @Override
    public void run(String... args) throws Exception {
        try {
            Resource resource = getFontResource();
            if (resource.exists()) {
                try (InputStream inputStream = resource.getInputStream()) {
                    int available = inputStream.available();
                    log.info("SimHei.ttf文件字节数: " + available);
                }
            } else {
                log.info("SimHei.ttf文件不存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}